<?php
set_time_limit(0);
require_once 'common.inc.php';
require_once 'config.inc.php';
require_once MCC_ROOT.'/include/autochinese.php';
error_reporting(0);
!in_array('1',$larr)&&adminmsg('你没有被赋予操作该项的权限！');
$matches=parse_url($_SERVER['HTTP_REFERER']);
//$url=$matches['scheme'].'://'.$matches['host'].'/';
$dsid    = new dbstuff();        // DS数据库连接标识符
$dsid->connect($dsdbhost,$dsdbuser,$dsdbpw,$dsdbname,1);

if($action =='search')
{
	$diskfree = (int)$diskfree;
	$fbtcid = (int)$fbtcid;
	!empty($uptime)&&$uptime = strtotime($uptime);
	!empty($corkey)&&$keyarr = explode('|',$corkey);
	$keyarr = array_filter($keyarr);
	$hotnum = (int)$hotnum;
	if(!empty($sum))
	{
		unset($wheresql);
	}
	if(!$wheresql)
		$wheresql = 'id>=1';
	else
	{
		$wheresql=stripslashes(urldecode($wheresql));
		$wheresql=preg_replace('/‖/','=',$wheresql);
	
	}
	if($uptime)
	{
		if($upp=='0')
		{
			$wheresql .= " AND UNIX_TIMESTAMP(uptime)='$uptime'";
		}
		elseif ($upp=='1')
		{
			$wheresql .= " AND UNIX_TIMESTAMP(uptime)>'$uptime'";
		}
		elseif ($upp=='-1')
		{
			$wheresql .= " AND UNIX_TIMESTAMP(uptime)<'$uptime'";
		}
	}
	$uptime = date('Y-m-d H:i:s',$uptime);
	if($keyarr)
	{
		$tmparr = array();
		foreach ($keyarr as $vl)
		{
			if(function_exists('mb_convert_encoding'))
				$vl = mb_convert_encoding($vl,'gbk','utf-8');
			else
			 	$vl=$covstr->Convert($vl);
			$tmparr[] = " seedname like '%$vl%' ";
		}
		$wheresql .= ' AND ('.implode('OR',$tmparr).')';
	}
	if($hotnum)
	{
		if($hotpp=='0')
		{
			$wheresql .= " AND clicktimes='$hotnum'";
		}
		elseif ($hotpp=='1')
		{
			$wheresql .= " AND clicktimes>'$hotnum'";
		}
		elseif ($hotpp=='-1')
		{
			$wheresql .= " AND clicktimes<'$hotnum'";
		}
		
	}
	if(is_numeric($ifcom))
	{
		if(1==$ifcom) $com1='checked="true"';
		if(0==$ifcom) $com2='checked="true"';
		$wheresql .= " AND completed='$ifcom'";
	}
	if(intval($id))
	{
		$id=intval($id);
		switch($equal)
		{
			case 1:$equal='>';break;
			case 2:$equal='=';break;
			case 3:$equal='<';break;
		}
		$wheresql .= " AND id".$equal."$id";
	}
	if($hash)
	{
		$wheresql .= " AND hash='".$hash."'";
	}
	elseif (ereg("[1-9]{1,2}",$fbtcid))//修改：将原来的1-6修改为1-9，但是仍然存在问题，上面要求条件放宽到1-13
	{
		$wheresql .= " AND btcid='".$fbtcid."'";
	}

	$nextsql=urlencode(preg_replace('/=/','‖',$wheresql));
	
	$limit=20;
	$page=intval($page)<1?1:intval($page);
	$start=(intval($page)-1)*$limit;
	$sql="select count(*) as num from localhash_1 where ".$wheresql;
	$dsid->query("set names 'latin1' ;");//如果出现查询乱码，可以试试这个
	//$dsid->query("set names 'utf8' ;");//如果出现查询乱码，可以试试这个

	$query=$dsid->query($sql);
	$count=$dsid->fetch_array($query);
	$allpage=ceil($count['num']/$limit);
	$page=$page>$allpage?$allpage:$page;
	$url='delebtc.inc.php?action='.$action.'&amp;wheresql='.$nextsql.'&amp;';
	$pages=numofpage($count['num'],$page,$allpage,$url);
	
	$delallsql='select * from localhash_1 where '. $wheresql.' order by uptime ';   //查询出全部结果；
	echo "<!--{$delallsql}-->";

	$wheresql.=" limit $start ,$limit";
	$sql = 'SELECT * FROM localhash_1 WHERE ' . $wheresql;

	//print $sql;
	$dsid->query("set names 'latin1' ;");//如果出现乱码，可以试试这个
	//$dsid->query("set names 'utf8' ;");//如果出现乱码，也可以试试这个
	
	$query = $dsid->query($sql);
	$delquery&&$delall = array();
	$delquery=$dsid->query($delallsql);
	$query&&$detedb = array();
	while ($rt=$dsid->fetch_array($query))
	{
		if(function_exists('mb_convert_encoding'))
			$rt['seedname'] = mb_convert_encoding($rt['seedname'],'utf-8','gbk');
		else
		   	$rt['seedname']=$covstr->Convert($rt['seedname']);
		$detedb[] = $rt;
	}
	
	while ($del=$dsid->fetch_array($delquery))
	{
		if(function_exists('mb_convert_encoding'))
			$del['seedname'] = mb_convert_encoding($del['seedname'],'utf-8','gbk');
		else
		   	$del['seedname']=$covstr->Convert($del['seedname']);
		$delall[] = $del;
		//print_r($del);
	}
	
//print_r($delall);
}
elseif ($action=='del')
{
	if(!empty($ifyellow)&&$ifyellow=='屏蔽')
	{
		foreach ($detehash as $hashl)
		{
			$dsid->query("update localhash_1 set ifyellow='1' where hash='$hashl'");
		}
		adminmsg('屏蔽帖子成功','delebtc.inc.php');
		exit;
	}
	foreach ($detehash as $hashl)
	{  
		$lhq = $dsid->query("select * from localhash_1 where hash='$hashl'");
		$lhrt = $dsid->fetch_array($lhq);
		if($lhrt)
		{
			$arears=$dsid->query("select ip,port from area where btcid='$lhrt[btcid]'");
			
			if($areart=$dsid->fetch_array($arears))
			{
				$btsenddata=pro_deletecontent($hashl);
				
				connecthost(long2ip($areart['ip']),$areart['port'],$btsenddata);//发送协议
			
			
			
			//	$otherno_86=connecthost('202.115.1.86',$areart['port'],$btsenddata);
				//$otherno_5=connecthost('211.162.191.5',$areart['port'],$btsenddata);
				/*
				$otherno=connecthost('202.115.1.87',$areart['port'],$btsenddata);
				$otherno_82=connecthost('202.115.1.82',$areart['port'],$btsenddata);				
				$otherno_81=connecthost('202.115.1.81',$areart['port'],$btsenddata);
				$otherno_88=connecthost('202.115.1.88',$areart['port'],$btsenddata);
				$otherno_89=connecthost('202.115.1.89',$areart['port'],$btsenddata);
				*/
			}		
			
		}
           //删除数据
         $p_infohash = trim($hashl);
		if(!isset($p_infohash) || strlen($p_infohash)<40 || empty($p_infohash))
		{
			exit('infohash is worng');
		}
			
		//删除三个东西：attach，hotfile,hothtml,还有同时也要删除attach中容量小于200的种子文件
		
		//删除具体文件
	   if(file_exists('../push/attach/'.$p_infohash.'.torrent'))
	   {
	   	 @unlink('../push/attach/'.$p_infohash.'.torrent');
		 //echo "<br>正在删除../push/attach/".$p_infohash.".torrent";
	   }		
		
		//读取attach文件夹所有文件，循环判断，容量小于200的则删除文件
		
		$dirhander = opendir('../push/attach');
		while ($file=readdir($dirhander))
		{
			if($file!='.'&&$file!='..')
			{
				if(filesize("../push/attach/$file")<200)
				{
					@unlink("../push/attach/$file");
					//echo "<br>正在删除../push/attach/$file……";
				}
			}
		}	
		
		//读取hotfile文件夹所有文件，循环删除文件
		
		$dirhander = opendir('../push/hotfile');
		while ($file=readdir($dirhander))
		{
			if($file!='.'&&$file!='..')
			{
				@unlink("../push/hotfile/$file");
			}
		}	
		//读取hothtml文件夹所有文件，循环删除文件
		
		$dirhander = opendir('../push/hothtml');
		while ($file=readdir($dirhander))
		{
			if($file!='.'&&$file!='..')
			{
				@unlink("../push/hothtml/$file");
			}
		}	

		$dsid->query("delete from localhash_1 where hash='$hashl'");
		
		
	}
	$nextsql=urlencode(preg_replace('/=/','‖',$wheresql));
	
	echo 'ISC 删除成功，网页正在跳转';
	echo "<meta http-equiv=\"refresh\" content=\"2;url=delebtc.inc.php?action=search&wheresql=$nextsql\">";
}
include_once MCC_ROOT.'/templates/msystem/delebtc.htm';
function prop_buildcontent($hash){
	$temp=chr(64).chr(1).chr(4);
	$temp.=$hash.".torrent\t\t\n";
	return $temp;
	
}

function pro_deletecontent($hash){
	$temp=chr(96).chr(1).chr(4);
	$temp.=$hash."\t\t\n";
	return $temp;
}

function connecthost($host,$port,$btcontent,$timeout=20){
	$fp=fsockopen($host,$port,$errno,$errstr,$timeout);
	//!$fp ?exit('不能连接服务器！！') : exit('suscc!');
	fwrite($fp,$btcontent);
	$serverdata=fgets($fp,1024);
	fclose($fp);
	return $serverdata.'|'.date("Y-m-d H:i:s")."\r\n";
}
?>
